package org.check.SSH;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;

import org.check.Util.XmlParser;
import org.check.VO.DBConnVO;
import org.check.VO.Server;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
/**
 * This program will demonstrate SSH through jump hosts.
 * Suppose that you don't have direct accesses to host2 and host3.
 *   $ CLASSPATH=.:../build javac JumpHosts.java
 *   $ CLASSPATH=.:../build java JumpHosts usr1@host1 usr2@host2 usr3@host3
 * You will be asked passwords for those destinations,
 * and if everything works fine, you will get file lists of your home-directory
 * at host3.
 *
 */
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

public class JumpHosts {
	public static int redirectPort = 3336;
	public static String localhost = "127.0.0.1";
	public static String nameSplitor = "\\$";
	public static int oraclePort = 1521;
	public static int sqlServerPort = 1433;
	private static Session session = null;
	
	public static void main(String[] arg){
		route2DB(XmlParser.generateDBVOList("conns-config.xml").get(0));
	}
	public static void route2DB(DBConnVO dbConnVO){
		int dbPort = 0;
		if(dbConnVO.getDbtype().equals("oracle"))
			dbPort = oraclePort;
		else if (dbConnVO.getDbtype().equals("mssql"))
			dbPort = sqlServerPort;
		try {
			  List<Server> list = dbConnVO.getServers();

			  JSch jsch = new JSch();
		      Server rootVO = list.get(0);

		      String host = rootVO.getHost();
		      String user = rootVO.getUser();
		      int port = rootVO.getPort();
		      String pwd = rootVO.getPwd();
		      Properties p = new Properties();
		      p.setProperty("StrictHostKeyChecking", "no");
		      session = jsch.getSession(user, host, port);
		      session.setConfig(p);
		      session.setPassword(pwd);
		      session.connect();
		      SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			  Date date = Calendar.getInstance().getTime();
			  String dateStr = format.format(date);
			  System.out.println(dateStr+"****"+"The session has been established to "+user+"@"+host);
		      
		      if(list.size()==1)
		    	  session.setPortForwardingL(dbPort, host, dbPort);	
		      for(int i = 1; i < list.size(); i++){
		    	  Server routeVO = list.get(i);
		    	  host = routeVO.getHost();
		          user = routeVO.getUser();
		          port = routeVO.getPort();
		          pwd = routeVO.getPwd();
		          session.setPortForwardingL(redirectPort+i, host, port);      
		          session = jsch.getSession(user, localhost, redirectPort+i);
		          session.setPassword(pwd);
		          session.setConfig(p);
		          
		          session.connect();
		          dateStr = format.format(Calendar.getInstance().getTime());
				  System.out.println(dateStr+"****"+"The session has been established to "+user+"@"+host);
		          //System.out.println(session.getServerVersion());
		          if(i == (list.size()-1))
		              session.setPortForwardingL(dbPort, host, dbPort);	          
		      }
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}	
	
}